<?php
namespace app\index\controller;
use app\common\model\Newsclass;
use app\common\model\Newsinfo;
use app\index\controller\Api as Api;
use think\Controller;
use think\Db;


class News extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 订单列表
     * @author lukui  2017-07-18
     * @return [type] [description]
     */
    public function lists($fid=0)
    {
        //$fid=!empty($_REQUEST['fid'])?intval($_REQUEST['fid']):0;
        $news_model = new Newsinfo();
        $newsclass_model = new Newsclass();
        $map_where  =array();
        $map_where['isdelete']=['=',0];
        if($fid>0){
            //$map_where['fid'] =$fid;
            $map_where['fid']=['=',$fid];
        }
        $fclass_name ='知识';
        $news_list = $news_model->where($map_where)->order('nid desc')->paginate(20);
        if($news_list){
            foreach ($news_list as $key=>$val){
                $news_list[$key]['ntime'] =date('Y-m-d H:i',$val['ntime']);
                $newsclass_info =$newsclass_model->where('fid',$val['fid'])->find();
                if($newsclass_info){
                    $fclass_name =$newsclass_info['fclass'];
                }
                $news_list[$key]['fclass'] =$fclass_name;
            }
        }

        if($fid>0){
            $newsclass_info =$newsclass_model->where('fid',$fid)->find();
            $fclass_name =$newsclass_info['fclass'];
        }
        $this->assign('fclass',$fclass_name);
        $this->assign('fid',$fid);
        $this->assign('news_list',$news_list);
        $this->assign('seo_title',$fclass_name.'-列表');
        return $this->fetch();

    }


    /**
     * 已平仓订单详情
     * @author lukui  2017-07-21
     * @return [type] [description]
     */
    public function show()
    {
        $id=!empty($_REQUEST['id'])?intval($_REQUEST['id']):0;
        $id = input('param.id');
        if(!$id){
            $this->redirect('/');
        }
        $news_model = new Newsinfo();
        $newsclass_model = new Newsclass();
        $news_info =$news_model->where('nid',$id)->find();
        $this->assign('news_info',$news_info);
        $newsclass_info =$newsclass_model->where('fid',$news_info['fid'])->find();
        $this->assign('fclass',$newsclass_info['fclass']);
        $this->assign('fid',$news_info['fid']);
        $this->assign('id',$news_info['nid']);
        $this->assign('seo_title',$newsclass_info['fclass'].'-'.$news_info['ntitle']);

        return $this->fetch();

    }
	/**
	 * ajax 通过产品id 获取用户订单，
	 * @author lukui  2017-07-22
	 * @return [type] [description]
	 */
	public function ajaxorder()
	{
		$uid = $_SESSION['uid'];
		$pid = input('param.pid');
		if (empty($uid) || empty($pid)) {
			return false;
		}
		//持仓信息
		$map = array('uid'=>$uid,'ostaus'=>0,'pid'=>$pid);
		$map['selltime'] = array('gt',time());
		$hold = Db::name('order')->where($map)->order('oid desc')->select();
		if($hold){
			$hold[0]['time'] = time();
		}
		if($hold){
			return $hold;
		}else{
			return false;
		}		
	}

	/**
	 * ajax 获取用户未平仓订单，
	 * @author lukui  2017-07-22
	 * @return [type] [description]
	 */
	public function ajaxorder_list()
	{
		$uid = $this->uid;
		if (empty($uid)) {
			return false;
		}
		//持仓信息
		$map = array('uid'=>$uid,'ostaus'=>0);
		$map['selltime'] = array('gt',time());

		$hold = Db::name('order')->where($map)->order('oid desc')->select();
		if($hold){
			$hold[0]['time'] = time();
		}
		if($hold){
			return $hold;
		}else{
			return false;
		}		
	}









	/**
	 * 实时获取以平仓订单
	 * @return [type] [description]
	 */
	public function get_this_order()
	{

		$oid = input('param.oid');
		$map['oid'] = $oid;
		$map['ostaus'] = 1;		
		$order = db('order')->where($map)->find();
	
		return $order;

	}

	/**
	 * 实时获取以平仓订单
	 * @return [type] [description]
	 */
	public function get_hold_order()
	{
	
		$oid = input('param.oid');
		$map['oid'] = $oid;
		$map['ostaus'] = 1;	
		$order = db('order')->where($map)->find();
		
		return $order;

	}





}
